<?php
/**
*
* @package phpBB3
* @version $Id$
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
*/

/**
* @ignore
*/
 
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include_once($phpbb_root_path . 'common.' . $phpEx);
include_once($phpbb_root_path . 'includes/functions_display.' . $phpEx);
include_once($phpbb_root_path . 'sv_common.' . $phpEx);
include_once($phpbb_root_path . 'ofc/php-ofc-library/open-flash-chart.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('stockvirtual');

$money = $user->data['user_money'];
$user_id 	= $user->data['user_id'];

if ($user_id == ANONYMOUS)
{
	login_box();
}

// draw trading data and user number.
draw_trading_data();

// draw visiting user num.
draw_visiting_user();

// list all delegations of all users in 72 hour. 
$global_list = false;

if ($auth->acl_getf_global('m_'))
{
	$request_time = get_taiwan_time() - 72*60*60;
	$sql 	= 'SELECT * FROM sv_transaction WHERE request_time>' . $request_time . ' ORDER BY request_time DESC';
	$result = $db->sql_query($sql); 
	while ($row = $db->sql_fetchrow($result))
	{
		if (is_valid_corp_id($row['corp_id']) == false)
		{
			continue;
		}
	
		$global_list = true;
	
		$deal_time = 0;
		if ((int)$row['deal_time'] != 0)
		{
			$deal_time = date('Y/m/d H:i:s', (int)$row['deal_time']);
		}
		$check_time = 0;
		if ((int)$row['check_time'] != 0)
		{
			$check_time = date('Y/m/d H:i:s', (int)$row['check_time']);
		}
		
		// Send vars to template
		$template->assign_block_vars('global_list', array(
			'USER_ID'						=> $row['user_id'],
			'USERNAME'						=> get_username($row['user_id']),
			'CORP_ID'						=> $row['corp_id'],
			'CORP_NAME'						=> get_corp_chinese_name($row['corp_id']),
			'NUMBER'						=> (int)$row['share']/1000,
			'ACTION'						=> $row['action'],
			'REQUEST_TIME'					=> date('Y/m/d H:i:s', (int)$row['request_time']),
			'PRICE'							=> $row['price'],
			'DEAL_TIME'						=> $deal_time,
			'NOTE'							=> $row['note'],
			'CHECK_TIME'					=> $check_time,
			'CHECK_STATUS'					=> $row['check_status'],
			)
		);
	}
}

display_forums('', $config['load_moderators']);

// include nav links.
include_nav_links();

// Assign index specific vars
$template->assign_vars(array(

	'FORUM_IMG'				=> $user->img('forum_read', 'NO_NEW_POSTS'),
	'FORUM_NEW_IMG'			=> $user->img('forum_unread', 'NEW_POSTS'),
	'FORUM_LOCKED_IMG'		=> $user->img('forum_read_locked', 'NO_NEW_POSTS_LOCKED'),
	'FORUM_NEW_LOCKED_IMG'	=> $user->img('forum_unread_locked', 'NO_NEW_POSTS_LOCKED'),

	'S_LOGIN_ACTION'			=> append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=login'),
	'S_DISPLAY_BIRTHDAY_LIST'	=> ($config['load_birthdays']) ? true : false,
	'U_MONEY'					=> number_format($money),
	'S_LIST'					=> $list,
	'JASON_STRING'				=> $jason_string,
	'VISITING_USER_JASON_STRING'=> $visiting_user_jason_string,
	
	'S_GLOBAL_LIST'				=> $global_list,
	'S_QUERY_USER'				=> append_sid("{$phpbb_root_path}query_user.$phpEx", 'mode=query_user'),
	
	'U_MARK_FORUMS'		=> ($user->data['is_registered'] || $config['load_anon_lastread']) ? append_sid("{$phpbb_root_path}index.$phpEx", 'hash=' . generate_link_hash('global') . '&amp;mark=forums') : '',
	'U_MCP'				=> ($auth->acl_get('m_') || $auth->acl_getf_global('m_')) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=main&amp;mode=front', true, $user->session_id) : '')
);

// Output page
page_header($user->lang['SV_HISTORY']);

$template->set_filenames(array(
	'body' => 'statistics.html')
);

page_footer();


function draw_visiting_user()
{
	global $visiting_user_jason_string, $db;

	$date			= array();
	$daily_value	= array();
	$weekly_value	= array();
	$monthly_value	= array();
	$max			= 0;
	$min			= 100000000000; // just pick a number big enough for now. 
	$num			= 0;
		
	$sql 	= 'SELECT * FROM sv_visiting_user';
	$result = $db->sql_query($sql);
	while ($row = $db->sql_fetchrow($result))
	{
		$date[]				= get_date_string($row['date']);
		$daily_value[]		= $daily		= (int)$row['daily_user_num'];
		$weekly_value[]		= $weekly		= (int)$row['weekly_user_num'];
		$monthly_value[]	= $monthly		= (int)$row['monthly_user_num'];
				
		// update the max/min of transaction number.
		if ($max < $monthly)
			$max = $monthly;
		if ($min > $daily)
			$min = $daily;	
		$num += 1;
	}
		
	// prepare open-flash-chart 
	$chart = new open_flash_chart();
	
	$title = new title('');
	$chart->set_title( $title );
	
	// set dot style for daily user number.
	$user_num_dot = new dot();
	$user_num_dot->size(3)->tooltip('#x_label#<br>Daily Users:#val#');
		
	// set line style for daily user number.
	$user_num_line	= new line();
	$user_num_line->set_colour( '#BFAFAF');
	$user_num_line->set_values( $daily_value );	
	$user_num_line->set_default_dot_style( $user_num_dot );		
	$user_num_line->attach_to_right_y_axis();
	$chart->add_element( $user_num_line );

	// set dot style for weekly user number.
	$weekly_user_num_dot = new dot();
	$weekly_user_num_dot->size(3)->tooltip('#x_label#<br>Weekly Users:#val#');
		
	// set line style for weekly user number.
	$weekly_user_num_line	= new line();
	$weekly_user_num_line->set_colour( '#7FAFAF');
	$weekly_user_num_line->set_values( $weekly_value );	
	$weekly_user_num_line->set_default_dot_style( $weekly_user_num_dot );		
	$weekly_user_num_line->attach_to_right_y_axis();
	$chart->add_element( $weekly_user_num_line );
	
	// set dot style for monthly user number.
	$monthly_user_num_dot = new dot();
	$monthly_user_num_dot->size(3)->tooltip('#x_label#<br>monthly Users:#val#');
		
	// set line style for monthly user number.
	$monthly_user_num_line	= new line();
	$monthly_user_num_line->set_colour( '#BF6FAF');
	$monthly_user_num_line->set_values( $monthly_value );	
	$monthly_user_num_line->set_default_dot_style( $monthly_user_num_dot );		
	$monthly_user_num_line->attach_to_right_y_axis();
	$chart->add_element( $monthly_user_num_line );	
	
	// set X labels.
	$x_labels = new x_axis_labels();
	$x_labels->set_steps( 1 );
	$x_labels->set_vertical();
	$x_labels->set_colour( '#A2ACBA' );
	$x_labels->set_labels( $date );
	
	$x = new x_axis();
	$x->set_colour( '#A2ACBA' );
	$x->set_grid_colour( '#D7E4A3' );
	$x->set_offset( false );
	$x->set_steps(1);
	
	// Add the X Axis Labels to the X Axis
	$x->set_labels( $x_labels ); 
	
	$chart->set_x_axis( $x );
	
	$y = new y_axis();
	$y->set_range( $min, $max, (int)(($max- $min)/5) ); //min, max, interval
	$chart->set_y_axis( $y );
			
	$visiting_user_jason_string = $chart->toPrettyString();	
}

function draw_trading_data()
{
	global $jason_string, $db;
	
	$date			= array();
	$value			= array();
	$max			= 0;
	$min			= 100000000000; // just pick a number big enough for now. 
	$num			= 0;
	$tran_num 		= 0;
	
	$daily_user_num		= array();
	$daily_user_id		= array();
	$weekly_user_num	= array();
	$weekly_user_id		= array();
	$monthly_user_num	= array();
	$monthly_user_id	= array();
	$max_user		= 0;
	$min_user		= 1000000;
	
	$tran_date 	= '';
	
	for ($i = 0; $i < 7; ++$i)
	{
		$weekly_user_id[$i] = array();
	}
	for ($i = 0; $i < 30; ++$i)
	{
		$monthly_user_id[$i] = array();
	}
	
	$sql 	= 'SELECT * FROM sv_transaction ORDER BY request_time ASC';
	$result = $db->sql_query($sql);
	while ($row = $db->sql_fetchrow($result))
	{
		if (is_valid_corp_id($row['corp_id']) == false)
		{
			continue;
		}
		
		if ($tran_date == '')
		{
			$tran_date = get_date_from_time((int)$row['request_time']);
		}
		
		// update user id list for every time interval.
		$id = $row['user_id'];
		$daily_user_id[$id] = 1;
		
		for ($i = 0; $i < 7; ++$i)
		{
			$weekly_user_id[$i][$id] = 1;
		}
		for ($i = 0; $i < 30; ++$i)
		{
			$monthly_user_id[$i][$id] = 1;
		}
		
		// Check if we move on to the next day.
		if ($tran_date == get_date_from_time((int)$row['request_time']))
		{
			$tran_num += 1;
		}
		else
		{
			// transaction date changes, update values for the date. 
			$date[] 			= get_date_string($tran_date);
			$value[]			= $tran_num;
			$daily_user_num[]	= sizeof($daily_user_id);
			$weekly_user_num[]	= sizeof($weekly_user_id[$num%7]);
			$monthly_user_num[]	= sizeof($monthly_user_id[$num%30]);
			
			// update the max/min of user number.
			$temp_user_num = sizeof($monthly_user_id[$num%30]);
			if ($max_user < $temp_user_num)
				$max_user = $temp_user_num;
			$temp_user_num = sizeof($daily_user_id);
			if ($min_user > $temp_user_num)
				$min_user = $temp_user_num;
				
			// update the max/min of transaction number.
			if ($max < $tran_num)
				$max = $tran_num;
			if ($min > $tran_num)
				$min = $tran_num;
			
			// reset.
			while( sizeof($daily_user_id) !=0 )
			{
				array_pop($daily_user_id);
			}
			$tran_num = 0;
			$temp_user_num = 0;
			$tran_date = get_date_from_time((int)$row['request_time']);
			
			// reset weekly user number.
			while( sizeof($weekly_user_id[$num%7]) !=0 )
			{
				array_pop($weekly_user_id[$num%7]);
			}			
	
			// reset monthly user number.
			while( sizeof($monthly_user_id[$num%30]) !=0 )
			{
				array_pop($monthly_user_id[$num%30]);
			}			
			
			$num += 1;
		}	
			
	}
	
	// update the data on last day.
	$date[] 			= get_date_string($tran_date);
	$value[]			= $tran_num;
	$daily_user_num[]	= sizeof($daily_user_id);
	$weekly_user_num[]	= sizeof($weekly_user_id[$num%7]);
	$monthly_user_num[]	= sizeof($monthly_user_id[$num%30]);
			
	$num		+= 1;
	
	if ($max < $tran_num)
		$max = $tran_num;
	if ($min > $tran_num)
		$min = $tran_num;
	
	// prepare open-flash-chart 
	$chart = new open_flash_chart();
	
	$title = new title('');
	$chart->set_title( $title );
	
	$area = new area();
	$area->set_colour( '#5B56B6' );
	$area->set_values( $value );
	$chart->add_element( $area );
	
	// set dot style for daily user number.
	$user_num_dot = new dot();
	$user_num_dot->size(3)->tooltip('#x_label#<br>Daily Users:#val#');
		
	// set line style for daily user number.
	$user_num_line	= new line();
	$user_num_line->set_colour( '#BFAFAF');
	$user_num_line->set_values( $daily_user_num );	
	$user_num_line->set_default_dot_style( $user_num_dot );		
	$user_num_line->attach_to_right_y_axis();
	$chart->add_element( $user_num_line );
	
	// set dot style for weekly user number.
	$weekly_user_num_dot = new dot();
	$weekly_user_num_dot->size(3)->tooltip('#x_label#<br>Weekly Users:#val#');
		
	// set line style for weekly user number.
	$weekly_user_num_line	= new line();
	$weekly_user_num_line->set_colour( '#7FAFAF');
	$weekly_user_num_line->set_values( $weekly_user_num );	
	$weekly_user_num_line->set_default_dot_style( $weekly_user_num_dot );		
	$weekly_user_num_line->attach_to_right_y_axis();
	$chart->add_element( $weekly_user_num_line );
	
	// set dot style for weekly user number.
	$monthly_user_num_dot = new dot();
	$monthly_user_num_dot->size(3)->tooltip('#x_label#<br>monthly Users:#val#');
		
	// set line style for weekly user number.
	$monthly_user_num_line	= new line();
	$monthly_user_num_line->set_colour( '#BF6FAF');
	$monthly_user_num_line->set_values( $monthly_user_num );	
	$monthly_user_num_line->set_default_dot_style( $monthly_user_num_dot );		
	$monthly_user_num_line->attach_to_right_y_axis();
	$chart->add_element( $monthly_user_num_line );
		
	// set X labels.
	$x_labels = new x_axis_labels();
	$x_labels->set_steps( 5 );
	$x_labels->set_vertical();
	$x_labels->set_colour( '#A2ACBA' );
	$x_labels->set_labels( $date );
	
	$x = new x_axis();
	$x->set_colour( '#A2ACBA' );
	$x->set_grid_colour( '#D7E4A3' );
	$x->set_offset( false );
	$x->set_steps(4);
	
	// Add the X Axis Labels to the X Axis
	$x->set_labels( $x_labels ); 
	
	$chart->set_x_axis( $x );
	
	$y = new y_axis();
	$y->set_range( $min, $max, (int)(($max- $min)/5) ); //min, max, interval
	$chart->set_y_axis( $y );
	
	$y_right = new y_axis_right();
	$y_right->set_range( $min_user, $max_user, (int)(($max_user - $min_user)/10));
	$y_right->set_stroke( 3 );
	$y_right->set_colour( '#BFAFAF' );
	$y_right->set_tick_length( 12 );
	$y_right->set_steps( (int)(($max_user - $min_user)/10));
	$chart->set_y_axis_right( $y_right );
		
	$jason_string = $chart->toPrettyString();	
}

?>